package com.anolesoft.yr.mbus.code.controller;

import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.base.dto.BaseAddressBookDTO;
import com.anolesoft.yr.mbus.base.dto.BaseCompanyDTO;
import com.anolesoft.yr.mbus.base.vo.BaseAddressBookVO;
import com.anolesoft.yr.mbus.base.vo.BaseCompanyVO;
import com.anolesoft.yr.mbus.code.dto.CodeItemDTO;
import com.anolesoft.yr.mbus.code.service.CodeItemService;
import com.anolesoft.yr.mbus.code.vo.CodeItemVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/21 11:50
 */
@RestController
@RequestMapping("/api/code/codeitem")
public class CodeItemController {
    @Autowired
    private CodeItemService codeItemService;
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(CodeItemVO codeItemVO) {
        CodeItemDTO codeItemDTO = vo2dto(codeItemVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, codeItemService.create(codeItemDTO));
    }
    @PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(CodeItemVO codeItemVO) {
        CodeItemDTO codeItemDTO = vo2dto(codeItemVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, codeItemService.modify(codeItemDTO));
    }

    @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, codeItemService.remove(id));
    }
    @DeleteMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, codeItemService.removeBatch(ids));
    }
    @GetMapping(value = "/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<CodeItemDTO> codeItemDTO = this.codeItemService.selectOneById(id);
        if (codeItemDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.codeItemDTO2VO(codeItemDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }
    @GetMapping(value = "/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(CodeItemVO codeItemVO) {
        CodeItemDTO codeItemDTO = CodeItemDTO.builder()
                .factoryCode(codeItemVO.getFactoryCode())
                .itemType(codeItemVO.getItemType())
                .itemCodeName(codeItemVO.getItemCodeName())
                .objuid(codeItemVO.getObjuid())
                .build();
        Integer limit = codeItemVO.getRows();
        Integer curr = codeItemVO.getPage();
        PageData<List<CodeItemDTO>> pageData = this.codeItemService.selectList(codeItemDTO, limit, curr);
        List<CodeItemVO> vos = pageData.getItems().stream().map(CustomBeanUtils::codeItemDTO2VO).collect(Collectors.toList());
        PageInfo<List<CodeItemVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    private CodeItemDTO vo2dto(CodeItemVO codeItemVO, boolean newId) {
        return CodeItemDTO.builder()
                .createTime(LocalDateTime.now())
                .createUserUid(SecurityUtils.getUserId())
                .deleteFlag(Boolean.FALSE)
                .factoryCode(SecurityUtils.getFactoryCode())
                .itemCodeName(codeItemVO.getItemCodeName())
                .itemDesc(codeItemVO.getItemDesc())
                .itemFormat(codeItemVO.getItemFormat())
                .itemGeneClass(codeItemVO.getItemGeneClass())
                .itemLength(codeItemVO.getItemLength())
                .itemPlaceholder(codeItemVO.getItemPlaceholder())
                .itemType(codeItemVO.getItemType())
                .modifyTime(LocalDateTime.now())
                .modifyUserUid(SecurityUtils.getUserId())
                .objuid(newId ? CommonUtils.fastUUID() : codeItemVO.getObjuid())
                .remark(codeItemVO.getRemark())
                .trimAspect(codeItemVO.getTrimAspect())
                .build();
    }
}
